import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { DictService } from '../../services/admin/dict.service';

/**
 * DictController
 * 对应 PHP: Dict Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('dict')
@Controller('adminapi/dict')
export class DictController {
  constructor(private readonly dictService: DictService) {}

  /**
   * 数据字典
   * 路由: GET dict
   * PHP路由: Route::get('dict', 'dict.Dict/lists')
   */
  @Get('dict')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.dictService.getPage(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: GET dict/:id
   * PHP路由: Route::get('dict/:id', 'dict.Dict/info')
   */
  @Get('dict/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async info(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.dictService.getInfo(id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: POST dict
   * PHP路由: Route::post('dict', 'dict.Dict/add')
   */
  @Post('dict')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async add(@Body() data: AddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: add

      return await this.dictService.add(data);
    } catch (error) {
      throw new BusinessException('add操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: PUT dict/:id
   * PHP路由: Route::put('dict/:id', 'dict.Dict/edit')
   */
  @Put('dict/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async edit(
    @Param('id') id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.dictService.edit(id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: DELETE dict/:id
   * PHP路由: Route::delete('dict/:id', 'dict.Dict/del')
   */
  @Delete('dict/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async del(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: del

      return await this.dictService.del(id);
    } catch (error) {
      throw new BusinessException('del操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: PUT dictionary/:id
   * PHP路由: Route::put('dictionary/:id', 'dict.Dict/addDictData')
   */
  @Put('dictionary/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async addDictData(
    @Param('id') id: string,
    @Body() data: AddDictDataDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: addDictData

      return await this.dictService.edit(id, data);
    } catch (error) {
      throw new BusinessException('addDictData操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: GET all
   * PHP路由: Route::get('all', 'dict.Dict/getAll')
   */
  @Get('all')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async getAll(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getAll

      return await this.dictService.getAll();
    } catch (error) {
      throw new BusinessException('getAll操作失败', error);
    }
  }

  /**
   * 数据字典
   * 路由: GET dictionary/type/:type
   * PHP路由: Route::get('dictionary/type/:type', 'dict.Dict/getKeyInfo')
   */
  @Get('dictionary/type/:type')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '数据字典' })
  async getKeyInfo(@Param('type') type: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getKeyInfo

      return await this.dictService.getKeyInfo(type);
    } catch (error) {
      throw new BusinessException('getKeyInfo操作失败', error);
    }
  }
}
